package com.whq.mvc.service.impl;

import com.whq.mvc.dao.UserDOMapper;
import com.whq.mvc.dao.UserPasswordDOMapper;
import com.whq.mvc.entity.UserDO;
import com.whq.mvc.entity.UserPasswordDO;
import com.whq.mvc.resp.BusinessException;
import com.whq.mvc.resp.EmBusinessError;
import com.whq.mvc.service.UserService;
import com.whq.mvc.service.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public UserModel getUserById(Integer id) {
        UserDO userDO = userDOMapper.selectByPrimaryKey(id);
        if (Objects.isNull(userDO)) return null;
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());

        return this.convertFromDataObject(userDO, userPasswordDO);
    }

    @Override
    public UserModel getUserByIdInCache(Integer id) {
        UserModel userModer = (UserModel) redisTemplate.opsForValue().get("user_validate_" + id);
        if (Objects.isNull(userModer)) {
            userModer = this.getUserById(id);
            redisTemplate.opsForValue().set("user_validate_" + id, userModer, 10, TimeUnit.MINUTES);
        }
        return userModer;
    }

    @Override
    @Transactional
    public void register(UserModel userModel) throws BusinessException {
        if (Objects.isNull(userModel)) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "用户信息为空");
        }
        UserDO userDO = this.convertFromModel(userModel);
        try {
            userDOMapper.insertSelective(userDO);
        } catch (DuplicateKeyException ex) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "该手机号已注册");
        }
        userModel.setId(userDO.getId());
        UserPasswordDO userPasswordDO = this.convertPasswordFromModel(userModel);
        userPasswordDOMapper.insert(userPasswordDO);
    }

    @Override
    public UserModel validateLogin(String telphone, String encrpPassword) throws BusinessException {
        UserDO userDO = userDOMapper.selectByTelphone(telphone);
        if (Objects.isNull(userDO)) throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        UserPasswordDO passwordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
        UserModel userModel = this.convertFromDataObject(userDO, passwordDO);
        if (!encrpPassword.equals(userModel.getEncrptPassword())) {
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }
        return userModel;
    }

    private UserDO convertFromModel(UserModel userModel) {
        if (Objects.isNull(userModel)) return null;
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;
    }

    private UserPasswordDO convertPasswordFromModel(UserModel userModel) {
        if (Objects.isNull(userModel)) return null;
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;
    }

    private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
        if (Objects.isNull(userDO)) return null;
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        if (Objects.nonNull(userPasswordDO)) {
            userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
        }
        return userModel;
    }
}
